/**
 * Created by leiqr on 2015/6/30.
 */
define(function (require, exports, module) {
    var $ = require('jquery');
   	var dialog =  require('dialog');
       var tip = require('js/tip');
    var d = dialog({
        title: '',
        width:480,
        content: [
            '<style>.input-text {box-sizing: border-box;width: 200px;background-color: #FFFFFF;background-image: none;border: 1px solid #d9d9d9;color: #5b5b60;padding: 5px 6px;height: 30px;font-size: 12px;line-height: 18px;-webkit-appearance: none;-moz-appearance: none;appearance: none;</style>',
            '<section id="index_top" class="index_top" style="padding: 30px 0 50px 0;">',
                    '<div class="row">',
                        '<div class="col-xs-12">',
                            '<div style="position: relative;width: 355px;margin: 0 auto;">',
                            '<p class="text-center" style="font-size: 20px;">帐号登录</p>',
                            '<p style="margin-top: 20px;line-height: 30px;"><span style="float: left;width: 100px;text-align: right">用户名：</span><input type="text" id="userName" placeholder="ekp账号名" class="input-text"></p>',
                            '<p style="margin-top: 20px;line-height: 30px;"><span style="float: left;width: 100px;text-align: right">密码：</span><input type="password" id="pwd" placeholder="ekp账号密码" class="input-text"></p>',
                                '<button type="submit" id="login_btn" class="btn btn-big mt15" style="background-color: #ff7713;color:#fff;width: 255px; margin-top: 30px; margin-left: 49px;">登录</button>',
                            '</div>',
                        '</div>',
                    '</div>',
            '</section>'
        ].join(''),
        cancel: false,
        ok:false,
        onshow: function(){
            var jsonpName = "jsonp_data";
            var doLogin = function (){
                var userName = $('#userName').val();
                var pwd = $('#pwd').val();
                if (userName === "") {
                    tip.showError("请输入用户名");
                    return false;
                }
                if (pwd === "") {
                    tip.showError("请输入密码");
                    return false;
                }

                $.getScript(api_domain + "/api/index.php?r=cms/site/login&userName=" + userName + "&pwd=" + pwd + "&jsonp=" + jsonpName)
                    .success(function(){
                        var res = window[jsonpName];
                        if (res){
                            if(res.retCode == 0) {
                                location.href = location.href;               
                            } else {
                                tip.showError(res.retMsg || res.errMsg);
                            }
                        }                                        
                    })
                    .fail(function(res){
                        
                    })
            }
            $('#login_btn').click(function(e){
                e.stopPropagation();
                doLogin();
            });
            $("#pwd").on('keypress', function (evt) {
                if (evt.which == 13) {
                    doLogin(); 
                }
            });
        }
    });
    exports.dialog = d;
});
